/*
 * The RavenX Library
 * Copyright (c) 2008 by Solidus
 * 
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.It is provided "as is" without express 
 * or implied warranty.
 *
 */

#ifndef RX_MISC_H_2008_01_21_00_32
#define RX_MISC_H_2008_01_21_00_32

#include "rx_typedef.h"

RX_SPACE_BEG



#define KB (1024)
#define MB (1024*KB)
#define GB (1024*MB)

#define RX_NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))	

#define RX_OFFSET_OF(type, member) ((size_t) &((type *)0)->member)


RX_INLINE void* RX_GET_ELEM(const void *ptr, size_t elem_size, size_t idx)
{
		return ((void*)((rx_byte*)ptr + ((idx) * elem_size)));
		
}


#define rx_max(a,b) ((a) > (b) ? (a) : (b))

#define rx_min(a,b) ((a) < (b) ? (a) : (b))





#define RX_BITMARK(pos)		((rx_uint64)(0x01) << pos)

#define RX_BITTEST(val, pos)  ((rx_bool)((val & (RX_BITMARK(pos)))))

#define RX_BITSET(val, pos)   (val |= (RX_BITMARK(pos)))

#define RX_BITCLR(val, pos)   (val &=  (~(RX_BITMARK(pos))))



RavenAPI rx_uint32 rx_rand32();
RavenAPI rx_uint64 rx_rand64();



RavenAPI rx_int16 rx_flip_int16(rx_int16 n);
RavenAPI rx_uint16 rx_flip_uint16(rx_uint16 n);


RavenAPI rx_int32 rx_flip_int32(rx_int32 n);
RavenAPI rx_uint32 rx_flip_uint32(rx_uint32 n);

RavenAPI rx_int64 rx_flip_int64(rx_int64 n);
RavenAPI rx_uint64 rx_flip_uint64(rx_uint64 n);




/**********************************hash***************************************/

RavenAPI rx_uint_n rx_hash_byte(const rx_byte *ptr, size_t n);

RavenAPI rx_uint_n rx_hash_str(const rx_char *str);

RavenAPI rx_uint_n rx_hash_wstr(const rx_wchar *str);



#if(ARCH == ARCH_IA32)

#define rx_rand_n		rx_rand32 
#define rx_flip_int_n	rx_flip_int32
#define rx_flip_uint_n	rx_flip_uint32

#elif(ARCH == ARCH_IA64 || ARCH == ARCH_X64)

#define rx_rand_n rx_rand64

#define rx_flip_int_n	rx_flip_int64
#define rx_flip_uint_n	rx_flip_uint64

#else

#error "Unknow Platform"

#endif




/***************************************************************************************************************/






#define RX_DECL_KEY_FUNC(type)			const void*		__macro_rx_##type##__key_func__(const void *ptr)
#define	RX_DECL_INIT_FUNC(type)			void			 __macro_rx_##type##__init_func__(void *ptr, size_t elem_size)
#define RX_DECL_UNINIT_FUNC(type)		void			 __macro_rx_##type##__uninit_func__(void *ptr, size_t elem_size)
#define RX_DECL_COPY_FUNC(type)			void			 __macro_rx_##type##__copy_func__(void *dest, const void *sour, size_t elem_size)
#define RX_DECL_CMP_FUNC(type)			int				__macro_rx_##type##__cmp_func__(const void *l, const void *r, size_t elem_size)
#define RX_DECL_SWAP_FUNC(type)			void			__macro_rx_##type##__swap_func(void *l, void *r, size_t elem_size)
#define RX_DECL_HASH_FUNC(type)			t_uint_n		__macro_rx_##type##__hash_func(const void *ptr, size_t elem_size);


#define RX_NAME_KEY_FUNC(type)			__macro_rx_##type##__key_func__
#define	RX_NAME_INIT_FUNC(type)			__macro_rx_##type##__init_func__
#define RX_NAME_UNINIT_FUNC(type)		__macro_rx_##type##__uninit_func__
#define RX_NAME_COPY_FUNC(type)			__macro_rx_##type##__copy_func__
#define RX_NAME_CMP_FUNC(type)			__macro_rx_##type##__cmp_func__
#define RX_NAME_SWAP_FUNC(type)			__macro_rx_##type##__swap_func
#define RX_NAME_HASH_FUNC(type)			__macro_rx_##type##__hash_func

#define RX_DECL_KV_PAIR(_key_, _value_) typedef struct __macro_rx_##_key_##_value_##_tag {_key_ key; _value_ value; }__macro_rx_##_key_##_value_##_t
#define RX_KV_PAIR_TYPE(_key_, _value_) __macro_rx_##_key_##_value_##_t




RX_SPACE_END

#endif

